<?php

namespace common\components\behaviors;

use yii\base\Behavior;
use yii\db\ActiveRecord;
use common\util\F;
use common\components\ActiveRecord2015;
use common\models\ModelLog;

/**
 * 记录模型的操作日志
 * @Description: 
 * @author: kai.gao
 * @date: 2014-11-24 下午7:10:12
 */
class LogBehavior extends Behavior {
	
	public function events() {
		return [
			ActiveRecord::EVENT_AFTER_INSERT => 'addLog',
			ActiveRecord::EVENT_AFTER_UPDATE => 'addLog',
			ActiveRecord::EVENT_AFTER_DELETE => 'addLog',
			ActiveRecord2015::EVENT_RUBBISH => 'addLog',
			ActiveRecord2015::EVENT_RESTORE => 'addLog'
		];
	}
	
	public function addLog($event) {
		/*$model = $this->owner;
		$attributes = [];
		switch ($event->name) {
			case ActiveRecord::EVENT_AFTER_INSERT:
					$attributes['type'] = '插入操作';break;
			case ActiveRecord::EVENT_AFTER_DELETE:
					$attributes['type'] = '删除操作';break;
			case ActiveRecord2015::EVENT_RUBBISH:
					$attributes['type'] = '回收操作';break;
			case ActiveRecord::EVENT_AFTER_UPDATE:
					$attributes['type'] = '修改操作';break;
			case ActiveRecord2015::EVENT_RESTORE:
			 		$attributes['type'] = '还原操作';break;
		}
		$attributes['class_name'] = get_class($model);
		$attributes['message'] = var_export($model->attributes, true);
		$attributes['server'] = var_export($_SERVER, true);
		$attributes['ip'] = F::IP();
		$attributes['log_time'] = date('Y-m-d H:i:s');
		if (\Yii::$app->id != 'app-console') {
		  $attributes['operator'] =  \Yii::$app->user->name;
		  $attributes['operator_id'] = \Yii::$app->user->id;
		  $attributes['sys'] = \Yii::$app->user->id;
		} else {
		    $attributes['operator'] = '计划任务';
		    $attributes['operator_id'] = '-1';
		    $attributes['sys'] = '-1';
		}
		$attributes['event_id'] = (int)$model->getPrimaryKey();
		$attributes['site_id'] = 1;
		
		$model = new ModelLog();
		$model->attributes = $attributes;
		$model->save();
		*/
	}

}

?>